//
//  ST_ShanYanConfigureMaker.swift
//  ShanYanAppStoreDemo
//
//  Created by wanglijun on 2019/8/13.
//  Copyright © 2019 wanglijun. All rights reserved.
//

import UIKit
import CL_ShanYanSDK

@objc protocol ST_ShanYanConfigureMakerDelegate: NSObjectProtocol {
    func shanYanConfigureOtherWayClick(sender: UIButton)
    func custom_nav_left_item_click(sender: UIButton)
}

class ST_ShanYanConfigureMaker: NSObject {
    
    var notif_observer : NSObject?
    weak var shanYanConfigureMakerDelegate :ST_ShanYanConfigureMakerDelegate?
    
    deinit {
        if let notif_observer = self.notif_observer {
            NotificationCenter.default.removeObserver(notif_observer)
        }
    }
    
    override init() {
        
    }
    init(delegate: ST_ShanYanConfigureMakerDelegate?) {
        shanYanConfigureMakerDelegate = delegate
    }
    //    MARK: - 绑定手机号 闪验模式
    
    func configure_bind_phone_number_style_yin_ke(configureInstance: CLUIConfigure) -> CLUIConfigure {
        let screenWidth_Portrait : CGFloat
        let screenHeight_Portrait : CGFloat
        let screenWidth_Landscape : CGFloat
        let screenHeight_Landscape : CGFloat
        
        let orientation = UIApplication.shared.statusBarOrientation
        if orientation == .portrait || orientation == .portraitUpsideDown{
            screenWidth_Portrait = UIScreen.main.bounds.width
            screenHeight_Portrait = UIScreen.main.bounds.height
            screenWidth_Landscape = UIScreen.main.bounds.height
            screenHeight_Landscape = UIScreen.main.bounds.width
        }else{
            screenWidth_Portrait = UIScreen.main.bounds.height
            screenHeight_Portrait = UIScreen.main.bounds.width
            screenWidth_Landscape = UIScreen.main.bounds.height
            screenHeight_Landscape = UIScreen.main.bounds.width
        }
        
        var screenScale = (UIScreen.main.bounds.width/375.0)
        if (screenScale > 1) {
            screenScale = 1
        }
        
        let style2Color = UIColor(red: 13/255.0, green: 79/255.0, blue: 254/255.0, alpha: 1)
        
        let baseUIConfigure = configureInstance;
        //layout 布局
        //布局-竖屏
        let clOrientationLayOutPortrait  = CLOrientationLayOut()
        
        
        baseUIConfigure.manualDismiss = NSNumber(booleanLiteral: true)
        
        baseUIConfigure.clBackgroundImg = UIImage.init(color: UIColor.white, size: CGSize.init(width: ScreenWidth, height: ScreenHeight))
        
        
        //横竖屏设置
        baseUIConfigure.shouldAutorotate = NSNumber(booleanLiteral: true)
        
        baseUIConfigure.supportedInterfaceOrientations = NSNumber(integerLiteral: Int(UIInterfaceOrientationMask.portrait.rawValue))
        //        baseUIConfigure.preferredInterfaceOrientationForPresentation = NSNumber(integerLiteral: Int(UIInterfaceOrientation.landscapeLeft.rawValue))
        //        baseUIConfigure.clAuthWindowModalTransitionStyle = NSNumber(integerLiteral: Int(UIModalTransitionStyle.crossDissolve.rawValue))
        
        /*
         baseUIConfigure.clPreferredStatusBarStyle = NSNumber(integerLiteral: Int(UIStatusBarStyle.lightContent.rawValue))
         baseUIConfigure.clNavigationBarStyle = NSNumber(integerLiteral: Int(UIBarStyle.black.rawValue))
         baseUIConfigure.clNavigationBackgroundClear = NSNumber(booleanLiteral: true)
         baseUIConfigure.clNavigationBottomLineHidden = NSNumber(booleanLiteral: true)
         */
        
        
        baseUIConfigure.clNavigationBarHidden = NSNumber(booleanLiteral: true)
        
        //        baseUIConfigure.clNavigationBackBtnImage = ST_ImageUtils.imageName("nav_button_white") ??
        //        baseUIConfigure.clNavigationLeftControl = UIBarButtonItem(image: ST_ImageUtils.imageName("nav_button_white")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(custom_nav_left_item_click(sender:)))
        
        //LOGO
        //        baseUIConfigure.clLogoImage = ST_ImageUtils.imageName("闪验logo2")!
        //        baseUIConfigure.clLogoHiden = NSNumber(booleanLiteral: true)
        
        
        
        
        //Slogan
        baseUIConfigure.clSloganTextColor = UIColor.init(hex: 0x999999)!
        baseUIConfigure.clSloganTextFont = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        baseUIConfigure.clSlogaTextAlignment = NSNumber(integerLiteral: Int(NSTextAlignment.left.rawValue))
        clOrientationLayOutPortrait.clLayoutSloganLeft = (30);
        let sloganTop = 106.cgFloat-20.cgFloat+statusBarHeight;
        clOrientationLayOutPortrait.clLayoutSloganTop = NSNumber.init(value: sloganTop.float)
        clOrientationLayOutPortrait.clLayoutSloganHeight = NSNumber(floatLiteral: 20)
        
        
        
        //PhoneNumber
        baseUIConfigure.clPhoneNumberColor = UIColor.init(hex: 0x333333)!
        baseUIConfigure.clPhoneNumberFont = UIFont.systemFont(ofSize: 24, weight: UIFont.Weight.semibold)
        
        let phoneTop = 217.cgFloat-20.cgFloat+statusBarHeight;
        clOrientationLayOutPortrait.clLayoutPhoneTop = NSNumber.init(value: phoneTop.float)
        clOrientationLayOutPortrait.clLayoutPhoneCenterX = (0)
        clOrientationLayOutPortrait.clLayoutPhoneHeight = NSNumber(floatLiteral: Double(33))
        //        clOrientationLayOutPortrait.clLayoutPhoneRight = NSNumber(floatLiteral: Double(-screenWidth_Portrait*0.15))
        //        clOrientationLayOutPortrait.clLayoutPhoneLeft = NSNumber(floatLiteral: Double(screenWidth_Portrait*0.15 + 70))
        
        
        
        //LoginBtn
        baseUIConfigure.clLoginBtnText = "本机号码一键登录"
        baseUIConfigure.clLoginBtnTextFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        baseUIConfigure.clLoginBtnBgColor = UIColor.init(hex: 0xFEC207)!
        baseUIConfigure.clLoginBtnCornerRadius = NSNumber(floatLiteral: Double(22))
        baseUIConfigure.clLoginBtnTextColor = UIColor.white
        //        baseUIConfigure.clLoginBtnShadowColor = UIColor(red: 180/255.0, green: 190/255.0, blue: 254/255.0, alpha: 1)
        //        baseUIConfigure.clLoginBtnshadowOpacity = NSNumber(floatLiteral: 0.8)
        //        baseUIConfigure.clLoginBtnshadowOffset = NSValue(cgSize: CGSize(width: 5, height: 5))
        //        baseUIConfigure.clLoginBtnMasksToBounds = NSNumber(booleanLiteral: false)
        clOrientationLayOutPortrait.clLayoutLoginBtnCenterX = NSNumber(floatLiteral: 0)
        let LoginBtnTop = 280.cgFloat-20.cgFloat+statusBarHeight;
        clOrientationLayOutPortrait.clLayoutLoginBtnTop = NSNumber.init(value: LoginBtnTop.float)
        
        clOrientationLayOutPortrait.clLayoutLoginBtnHeight = NSNumber(floatLiteral: Double(44))
        clOrientationLayOutPortrait.clLayoutLoginBtnWidth = NSNumber(floatLiteral: 260)
        
        
        
        
        
        
        
        
        
        //Privacy
        baseUIConfigure.clAppPrivacyFirst = ["伴心用户协议",PH.userProtocolURL]
        baseUIConfigure.clAppPrivacySecond = ["伴心隐私政策",PH.privacyProtocolURL]
        
        baseUIConfigure.clAppPrivacyColor = [UIColor.init(hex: 0xACB4C0)!,UIColor.init(hex: 0xACB4C0)!]
        baseUIConfigure.clAppPrivacyTextAlignment = NSNumber(integerLiteral: Int(NSTextAlignment.center.rawValue))
        baseUIConfigure.clAppPrivacyTextFont = UIFont.systemFont(ofSize: 11)
        //        baseUIConfigure.clAppPrivacyLineSpacing = @(2);
        //        baseUIConfigure.clAppPrivacyNeedSizeToFit = @(YES);
        clOrientationLayOutPortrait.clLayoutAppPrivacyLeft = NSNumber(floatLiteral: Double(60))
        clOrientationLayOutPortrait.clLayoutAppPrivacyRight = NSNumber(floatLiteral: Double(-39))
        clOrientationLayOutPortrait.clLayoutAppPrivacyBottom = NSNumber(floatLiteral: Double(-BTSafeAreaBottom - 15.cgFloat))
        //        clOrientationLayOutPortrait.clLayoutAppPrivacyHeight = NSNumber(floatLiteral: Double(32))
        
        
        //CheckBox
        //CheckBox
        baseUIConfigure.clCheckBoxHidden = NSNumber(booleanLiteral: false)
        baseUIConfigure.clCheckBoxValue = NSNumber(booleanLiteral: true)
        baseUIConfigure.clCheckBoxCheckedImage = ST_ImageUtils.imageName("checkboxround1")!
        baseUIConfigure.clCheckBoxUncheckedImage = ST_ImageUtils.imageName("checkboxround0-2")!
        baseUIConfigure.clCheckBoxSize = NSValue.init(cgSize: CGSize(width: 16, height: 16))
        //        baseUIConfigure.clCheckBoxImageEdgeInsets = NSValue(uiEdgeInsets: UIEdgeInsets(top: 8, left: 14, bottom: 6, right: 0))
        baseUIConfigure.clCheckBoxVerticalAlignmentOffset = NSNumber(value: 0)
        
        
        
        
        clOrientationLayOutPortrait.clAuthWindowOrientationOrigin = NSValue(cgPoint: CGPoint.zero)
        clOrientationLayOutPortrait.clAuthWindowOrientationWidth = NSNumber(floatLiteral: Double(screenWidth_Portrait))
        clOrientationLayOutPortrait.clAuthWindowOrientationHeight = NSNumber(floatLiteral: Double(screenHeight_Portrait))
        
        baseUIConfigure.clOrientationLayOutPortrait = clOrientationLayOutPortrait;
        
        
        
        baseUIConfigure.customAreaView =  { customAreaView in
            
            customAreaView.backgroundColor = UIColor.random
            
            let otherLinkBtn = UIButton()
            
            otherLinkBtn.setTitle("切换短信验证", for: .normal)
            otherLinkBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            otherLinkBtn.setTitleColor(UIColor.init(hex: 0x333333), for: .normal)
            customAreaView.addSubview(otherLinkBtn)
            otherLinkBtn.addTarget(self, action: #selector(self.other_way_loginbtn_click(sender:)), for: .touchUpInside)
            
            otherLinkBtn.snp.makeConstraints { (maker) in
                maker.centerX.equalToSuperview()
                maker.height.equalTo(30)
                maker.top.equalToSuperview().offset(354-20+statusBarHeight)
            }
            
            let phone_icon_view:UIImageView = {
                let control = UIImageView()
                control.contentMode = UIView.ContentMode.scaleAspectFill
                control.image = ST_ImageUtils.imageName("register_icon_emoji电话")
                return control
            }()
            
            let phone_num_sub_label:UILabel = {
                let control = UILabel.init()
                control.text = "我们需要你的电话号码以识别身份"
                control.textColor = UIColor.init(hexString: "#333333")
                control.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
                return control
            }()
            
            customAreaView.addSubview(phone_icon_view)
            
            phone_icon_view.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(30)
                make.top.equalToSuperview().offset(72-20+statusBarHeight)
                make.width.equalTo(24)
                make.height.equalTo(24)
            }
            
            customAreaView.addSubview(phone_num_sub_label)
            phone_num_sub_label.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(60)
                make.centerY.equalTo(phone_icon_view)
                make.width.equalTo(270)
                make.height.equalTo(24)
            }
            //            CGRect.init(origin: CGPoint.init(x: ScreenWidth, y: 72-20+statusBarHeight), size: CGSize.init(width: 24, height: 24))
            
            //            otherLinkBtn.hero.modifiers = [.translate(x: 0, y: 20, z: 0),.fade]
            
        }
        
        
        return baseUIConfigure;
    }
    
    // MARK: - 视频背景沉浸模式
    func configure_style_yin_ke(configureInstance: CLUIConfigure,agreePrivacy:Bool) -> CLUIConfigure {
        let screenWidth_Portrait : CGFloat
        let screenHeight_Portrait : CGFloat
        let screenWidth_Landscape : CGFloat
        let screenHeight_Landscape : CGFloat

        let orientation = UIApplication.shared.statusBarOrientation
        if orientation == .portrait || orientation == .portraitUpsideDown{
            screenWidth_Portrait = UIScreen.main.bounds.width
            screenHeight_Portrait = UIScreen.main.bounds.height
            screenWidth_Landscape = UIScreen.main.bounds.height
            screenHeight_Landscape = UIScreen.main.bounds.width
        }else{
            screenWidth_Portrait = UIScreen.main.bounds.height
            screenHeight_Portrait = UIScreen.main.bounds.width
            screenWidth_Landscape = UIScreen.main.bounds.height
            screenHeight_Landscape = UIScreen.main.bounds.width
        }
        
        var screenScale = (UIScreen.main.bounds.width/375.0)
        if (screenScale > 1) {
            screenScale = 1
        }
        
        
        
        let baseUIConfigure = configureInstance;
        
        //layout 布局
        //布局-竖屏
        let clOrientationLayOutPortrait  = CLOrientationLayOut()
        
        
        baseUIConfigure.manualDismiss = NSNumber(booleanLiteral: true)

        baseUIConfigure.clBackgroundColor = .clear
        
        //横竖屏设置
        baseUIConfigure.shouldAutorotate = NSNumber(booleanLiteral: true)
        
        baseUIConfigure.supportedInterfaceOrientations = NSNumber(integerLiteral: Int(UIInterfaceOrientationMask.portrait.rawValue))
        
        baseUIConfigure.clAuthWindowModalPresentationStyle = NSNumber(integerLiteral: Int(UIModalPresentationStyle.overCurrentContext.rawValue))
        
        //        baseUIConfigure.preferredInterfaceOrientationForPresentation = NSNumber(integerLiteral: Int(UIInterfaceOrientation.landscapeLeft.rawValue))
        //        baseUIConfigure.clAuthWindowModalTransitionStyle = NSNumber(integerLiteral: Int(UIModalTransitionStyle.crossDissolve.rawValue))
        
        /*
         baseUIConfigure.clPreferredStatusBarStyle = NSNumber(integerLiteral: Int(UIStatusBarStyle.lightContent.rawValue))
         baseUIConfigure.clNavigationBarStyle = NSNumber(integerLiteral: Int(UIBarStyle.black.rawValue))
         baseUIConfigure.clNavigationBackgroundClear = NSNumber(booleanLiteral: true)
         baseUIConfigure.clNavigationBottomLineHidden = NSNumber(booleanLiteral: true)
         */
        
        
        baseUIConfigure.clNavigationBarHidden = NSNumber(booleanLiteral: true)
        
        //        baseUIConfigure.clNavigationBackBtnImage = ST_ImageUtils.imageName("nav_button_white") ??
        //        baseUIConfigure.clNavigationLeftControl = UIBarButtonItem(image: ST_ImageUtils.imageName("nav_button_white")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(custom_nav_left_item_click(sender:)))
        
        //LOGO
        //        baseUIConfigure.clLogoImage = ST_ImageUtils.imageName("闪验logo2")!
        //        baseUIConfigure.clLogoHiden = NSNumber(booleanLiteral: true)
        
        
        
        
        //Slogan
        baseUIConfigure.clSloganTextColor = UIColor.clear
        //        baseUIConfigure.clSloganTextFont = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        //        baseUIConfigure.clSlogaTextAlignment = NSNumber(integerLiteral: Int(NSTextAlignment.left.rawValue))
        //        clOrientationLayOutPortrait.clLayoutSloganLeft = (30);
        //        let sloganTop = 106.cgFloat-20.cgFloat+statusBarHeight;
        //        clOrientationLayOutPortrait.clLayoutSloganTop = NSNumber.init(value: sloganTop.float)
        //        clOrientationLayOutPortrait.clLayoutSloganHeight = NSNumber(floatLiteral: 20)
        
        
        
        //PhoneNumber
        baseUIConfigure.clPhoneNumberColor = UIColor.init(hex: 0x333333)!
        baseUIConfigure.clPhoneNumberFont = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        
        clOrientationLayOutPortrait.clLayoutPhoneBottom = NSNumber.init(value: (-203-BTSafeAreaBottom-10).float)
        clOrientationLayOutPortrait.clLayoutPhoneCenterX = (0)
        clOrientationLayOutPortrait.clLayoutPhoneHeight = NSNumber(floatLiteral: Double(28))
        //        clOrientationLayOutPortrait.clLayoutPhoneRight = NSNumber(floatLiteral: Double(-screenWidth_Portrait*0.15))
        //        clOrientationLayOutPortrait.clLayoutPhoneLeft = NSNumber(floatLiteral: Double(screenWidth_Portrait*0.15 + 70))
        
        
        
        //LoginBtn
        baseUIConfigure.clLoginBtnNormalBgImage = ST_ImageUtils.imageName("sy_login_award_icon")!
        baseUIConfigure.clLoginBtnText = ""
        baseUIConfigure.clLoginBtnTextFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
//        baseUIConfigure.clLoginBtnBgColor = UIColor.init(hex: 0xFEC207)!
//        baseUIConfigure.clLoginBtnCornerRadius = NSNumber(floatLiteral: Double(24))
//        baseUIConfigure.clLoginBtnTextColor = UIColor.white
        //        baseUIConfigure.clLoginBtnShadowColor = UIColor(red: 180/255.0, green: 190/255.0, blue: 254/255.0, alpha: 1)
        //        baseUIConfigure.clLoginBtnshadowOpacity = NSNumber(floatLiteral: 0.8)
        //        baseUIConfigure.clLoginBtnshadowOffset = NSValue(cgSize: CGSize(width: 5, height: 5))
        //        baseUIConfigure.clLoginBtnMasksToBounds = NSNumber(booleanLiteral: false)
        clOrientationLayOutPortrait.clLayoutLoginBtnCenterX = NSNumber(floatLiteral: 0)
        let LoginBtnTop = 145+BTSafeAreaBottom+10;
        clOrientationLayOutPortrait.clLayoutLoginBtnBottom = NSNumber.init(value: -LoginBtnTop.float)
        
        clOrientationLayOutPortrait.clLayoutLoginBtnHeight = NSNumber(floatLiteral: Double(48))
        clOrientationLayOutPortrait.clLayoutLoginBtnWidth = NSNumber(floatLiteral: 260)
        
        
        
        
        
        
        
        
        
        //Privacy
        baseUIConfigure.clAppPrivacyFirst = ["伴心用户协议",PH.userProtocolURL]
        baseUIConfigure.clAppPrivacySecond = ["伴心隐私政策",PH.privacyProtocolURL]
        
        baseUIConfigure.clAppPrivacyColor = [UIColor.init(hex: 0xACB4C0)!,UIColor.init(hex: 0xACB4C0)!]
        baseUIConfigure.clAppPrivacyTextAlignment = NSNumber(integerLiteral: Int(NSTextAlignment.center.rawValue))
        baseUIConfigure.clAppPrivacyTextFont = UIFont.systemFont(ofSize: 11)
        //        baseUIConfigure.clAppPrivacyLineSpacing = @(2);
        //        baseUIConfigure.clAppPrivacyNeedSizeToFit = @(YES);
        clOrientationLayOutPortrait.clLayoutAppPrivacyLeft = NSNumber(floatLiteral: Double(60))
        clOrientationLayOutPortrait.clLayoutAppPrivacyRight = NSNumber(floatLiteral: Double(-39))
        clOrientationLayOutPortrait.clLayoutAppPrivacyBottom = NSNumber(floatLiteral: Double(-BTSafeAreaBottom - 10.cgFloat))
        //        clOrientationLayOutPortrait.clLayoutAppPrivacyHeight = NSNumber(floatLiteral: Double(32))
        
        
        //CheckBox
        //CheckBox
        baseUIConfigure.clCheckBoxHidden = NSNumber(booleanLiteral: false)
        baseUIConfigure.clCheckBoxValue = NSNumber(booleanLiteral: agreePrivacy)
        baseUIConfigure.clCheckBoxCheckedImage = ST_ImageUtils.imageName("checkboxround1")!
        baseUIConfigure.clCheckBoxUncheckedImage = ST_ImageUtils.imageName("checkboxround0-2")!
        baseUIConfigure.clCheckBoxSize = NSValue.init(cgSize: CGSize(width: 16, height: 16))
        //        baseUIConfigure.clCheckBoxImageEdgeInsets = NSValue(uiEdgeInsets: UIEdgeInsets(top: 8, left: 14, bottom: 6, right: 0))
        baseUIConfigure.clCheckBoxVerticalAlignmentOffset = NSNumber(value: 0)
        
        
        
        
        clOrientationLayOutPortrait.clAuthWindowOrientationOrigin = NSValue(cgPoint: CGPoint.zero)
        clOrientationLayOutPortrait.clAuthWindowOrientationWidth = NSNumber(floatLiteral: Double(screenWidth_Portrait))
        clOrientationLayOutPortrait.clAuthWindowOrientationHeight = NSNumber(floatLiteral: Double(screenHeight_Portrait))
        
        baseUIConfigure.clOrientationLayOutPortrait = clOrientationLayOutPortrait;
        
        
        
        baseUIConfigure.customAreaView =  { customAreaView in
            
            
            customAreaView.backgroundColor = .clear
            
            let otherLinkBtn = UIButton()
            
            otherLinkBtn.setTitle("切换其他号码 >", for: .normal)
            otherLinkBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            otherLinkBtn.setTitleColor(UIColor.init(hex: 0x333333), for: .normal)
            customAreaView.addSubview(otherLinkBtn)
            otherLinkBtn.addTarget(self, action: #selector(self.other_way_loginbtn_click(sender:)), for: .touchUpInside)
            
            otherLinkBtn.snp.makeConstraints { (maker) in
                maker.centerX.equalToSuperview()
                maker.height.equalTo(30)
                maker.bottom.equalToSuperview().offset(-115-BTSafeAreaBottom-10)
            }
            
        }
        
        
        return baseUIConfigure;
    }
    
    
    // MARK: - other_way_loginbtn_click
    @objc func other_way_loginbtn_click(sender: UIButton){
        if let delegate = self.shanYanConfigureMakerDelegate {
            delegate.shanYanConfigureOtherWayClick(sender: sender)
        }
    }
    @objc func custom_nav_left_item_click(sender: UIButton){
        
        if let delegate = self.shanYanConfigureMakerDelegate {
            delegate.custom_nav_left_item_click(sender: sender)
        }
    }
}
